<?php

namespace App\Http\Controllers\CmsApi;

use App\Http\Controllers\BaseController;
use App\Http\Model\Hint;
use App\Http\Model\OutJson;
use App\Http\Model\Room;
use App\Http\Model\RoomManualManagement;
use Illuminate\Http\Request;

class RoomManualManagementController extends BaseController
{
    /**
     * 获取所有手动推荐房间
     */
    public function getAll()
    {
        $all = RoomManualManagement::query()->select(['id', 'platform', 'room_uid_list'])->get()->toArray();
        OutJson::outJsonResult(Hint::SUCCESS, $all);
    }

    /**
     * 增加
     *
     * @param Request $request
     */
    public function add(Request $request)
    {
        $messages = ['platform.required' => '缺少参数：渠道', 'room_uid_list.required' => '缺少参数：房间UID'];
        $rules = ['platform' => 'required', 'room_uid_list' => 'required',];
        $params = parent::validateParams($request->all(), $rules, $messages);
        $platform = strval($params['platform']);//渠道
        $roomUIds = $params['room_uid_list'];//房间uid
        $platformRecord = RoomManualManagement::query()->where('platform', $platform)->count();//记录是否存在
        if ($platformRecord) {
            OutJson::outJsonCustom(Hint::ERROR, '渠道记录已存在');
        }

        $roomUIds = explode(',', $roomUIds);
        $tempRoomUuids = [];
        foreach ($roomUIds as $roomUId) {
            $roomUId = intval(trim($roomUId));
            if (!$roomUId) {
                continue;
            }

            $tempRoomUuids[] = $roomUId;
        }

        if ($tempRoomUuids) {
            $tempRoomUuids = array_unique($tempRoomUuids);
            $existsRoomUuids = Room::query()->select(['room_uid'])->whereIn('room_uid', $tempRoomUuids)->pluck('room_uid')->toArray();
            if ($existsRoomUuids) {
                RoomManualManagement::query()->create(['platform' => $platform, 'room_uid_list' => implode(',', $existsRoomUuids)]);
                (new RoomManualManagement())->clearAndSetCache($platform);//清除缓存
            }
        }

        OutJson::outJsonResult(Hint::SUCCESS);
    }

    /**
     * 修改
     *
     * @param Request $request
     */
    public function update(Request $request)
    {
        $messages = ['id.required' => '缺少参数:ID', 'id.exists' => '记录不存在', 'platform.required' => '缺少参数：渠道', 'room_uid_list.required' => '缺少参数：房间UID'];
        $rules = ['id' => 'required|exists:room_manual_management', 'platform' => 'required', 'room_uid_list' => 'required',];
        $params = parent::validateParams($request->all(), $rules, $messages);
        $record = RoomManualManagement::query()->find($params['id'])->toArray();//记录
        $roomUIds = $params['room_uid_list'];//房间uid
        $roomUIds = explode(',', $roomUIds);
        $tempRoomUuids = [];
        foreach ($roomUIds as $roomUId) {
            $roomUId = intval(trim($roomUId));
            if (!$roomUId) {
                continue;
            }

            $tempRoomUuids[] = $roomUId;
        }

        if ($tempRoomUuids) {
            $tempRoomUuids = array_unique($tempRoomUuids);
            $existsRoomUuids = Room::query()->select(['room_uid'])->whereIn('room_uid', $tempRoomUuids)->pluck('room_uid')->toArray();
            RoomManualManagement::query()->where('id', $params['id'])->update(['platform' => $params['platform'], 'room_uid_list' => implode(',', $existsRoomUuids)]);
            (new RoomManualManagement())->clearAndSetCache($record['platform']);//清除缓存
        }

        OutJson::outJsonResult(Hint::SUCCESS);
    }

    /**
     * 删除
     *
     * @param Request $request
     */
    public function delete(Request $request)
    {
        $messages = ['id.required' => '缺少参数:ID', 'id.exists' => '记录不存在'];
        $rules = ['id' => 'required|exists:room_manual_management',];
        $params = parent::validateParams($request->all(), $rules, $messages);
        $record = RoomManualManagement::query()->find($params['id'], ['platform'])->toArray();
        RoomManualManagement::query()->where('id', $params['id'])->delete();
        (new RoomManualManagement())->clearAndSetCache($record['platform']);//清除缓存
        OutJson::outJsonResult(Hint::SUCCESS);
    }
}